const mysql = require('../../core/mysql');

const log = require('../../core/logger').getLogger("system");
const moment = require('moment');
const common = require('../../core/common');
const stringUtils = require('../../core/util/StringUtils');
const _ = require('lodash');

exports.add = async(ctx) => {
    let icon_type = ctx.request.body.icon_type || -1;
    let pic_url = ctx.request.body.pic_url || "";
    let icon_sign = ctx.request.body.icon_sign || "";



    if (icon_type <= -1 || pic_url === '' || icon_sign === '' ) {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
        return;
    }
    let newicon = {
        icon_type,
        pic_url,
        icon_sign,
        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        update_time: moment().format('YYYY-MM-DD HH:mm:ss'),
    };
    try{
        ret = await mysql.query(`INSERT INTO icon SET ?`, newicon);
        if (!ret || ret.insertId === -1){
            ctx.body = {
                result: 202,
                message: "新增图标失败"
            };
        } else {
            ctx.body = {
                result: 200,
                id: ret.insertId,
                message: "新增图标成功"
            };
        }
    }catch (e) {
        ctx.body = {
            result: 203,
            message: "新增图标失败"+e.toString()
        };
    }
}

exports.delete = async(ctx) => {
    let icon_id = ctx.params.icon_id || 0;
    if (icon_id <= 0) {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
        return
    }
    try{
        ret = await mysql.query(`delete from icon WHERE icon_id = ?`, icon_id);
        console.log(ret);
        if (!ret || ret.insertId === -1){
            ctx.body = {
                result: 202,
                message: "删除失败"
            };
        } else {
            ctx.body = {
                result: 200,
                message: "删除成功"
            };
        }
    }catch (e) {
        ctx.body = {
            result: 203,
            message: "删除失败"+e.toString()
        };
    }
}

exports.update = async(ctx) => {
    let icon_id = ctx.request.body.icon_id || 0;
    let pic_url = ctx.request.body.pic_url || '';
    let icon_sign = ctx.request.body.icon_sign || '';
    let icon_type = ctx.request.body.icon_type || '';

    if (icon_id <= 0 || pic_url === '' || icon_sign === '' || icon_type === '') {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
    }else{
        try {
            let selectRows = await mysql.query(`SELECT * FROM icon WHERE icon_id = ?`, icon_id);
            if (selectRows.length > 0) {
                console.log('selectRows:', selectRows);
                selectRows[0].pic_url = pic_url;
                selectRows[0].icon_sign = icon_sign;
                selectRows[0].icon_type = icon_type;
                selectRows[0].update_time = moment().format('YYYY-MM-DD HH:mm:ss');
                ret = await mysql.query(`UPDATE icon SET ? WHERE icon_id = ?`, [selectRows[0],icon_id]);
                console.log(ret);
                if (!ret || ret.insertId === -1) {
                    ctx.body = {
                        result: 202,
                        message: "更新失败"
                    };
                } else {
                    ctx.body = {
                        result: 200,
                        message: "更新成功"
                    };
                }
            } else {
                ctx.body = {
                    result: 203,
                    message: "对象不存在或者已删除"
                };
            }
        }catch (e) {
            ctx.body = {
                result: 204,
                message: "更新失败"+e.toString()
            };
        }
    }
}


exports.getList = async(ctx) => {
    let create_user_id = ctx.user.id || '';
    let counts = ctx.query.counts || 10;
    let indexfrom = ctx.query.indexfrom || 0;

    if (create_user_id === ''){
        ctx.body = {
            result: 201,
            message: "无效的用户id"
        };
    } else {
        try {
            let results = await mysql.query(`SELECT * FROM icon ORDER BY icon_id LIMIT ${indexfrom}, ${counts} ` );
            ctx.body = {
                result: 200,
                list:results,
                message: "查询成功"
            };
        }catch (e) {
            ctx.body = {
                result: 205,
                message: "数据库错误"
            };
        }
    }
}